package com.tsmti.webservice.util;

import com.alibaba.fastjson.JSONObject;
import com.tsmti.webservice.constants.Constants;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Title：pims
 * Class：com.pims.webservice.common
 * Comapny:杭州同烁信息技术有限公司
 *
 * @author：周陈伟
 * @version：2.5 CreateTime：2017/9/27 11:31
 * Modify log:
 * Description：
 */
public class Util {

    /**
     * 获取年龄 及年龄单位
     * @param birthDay
     * @return
     * @throws Exception
     */
    public static JSONObject getAge(String birthDay) {
        JSONObject jsonAge = new JSONObject();
        try {
            long day=getDays(birthDay, getStringDate());
            long year=day/365;
            long month=(day-365L*year)/30;
            long week=(day-month* 30L)/7;
            if(year>1){
                jsonAge.put("age",year);
                jsonAge.put("ageUnit","岁");
            }else if(month>0){
                if(year==1) {
                    month += 12L;
                }
                jsonAge.put("age",month);
                jsonAge.put("ageUnit","月");
            }else if(week>0){
                jsonAge.put("age",week);
                jsonAge.put("ageUnit","周");
            }else {
                jsonAge.put("age",day);
                jsonAge.put("ageUnit","天");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return jsonAge;
    }

    public static String getStringDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }
    /**
     * 两个时间之间的天数
     */
    public static long getDays(String date1, String date2) {
        if (date1 == null || "".equals(date1)) {
            return 0;
        }
        if (date2 == null || "".equals(date2)) {
            return 0;
        }
        // 转换为标准时间
        SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        Date mydate = null;
        try {
            date = myFormatter.parse(date1);
            mydate = myFormatter.parse(date2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
        return Math.abs(day);
    }

    /***
     * 阴阳性结果转换
     * @param result
     * @return
     */
    public static String convertResult(String result) {
        String returnResult = "";
        if (result.indexOf(Constants.LOWER_POSITIVE) > -1 || result.indexOf(Constants.UPPER_POSITIVE) > -1
                || result.indexOf(Constants.CH_POSITIVE) > -1 || result.indexOf(Constants.EN_POSITIVE) > -1) {
            returnResult = "阳性";
        } else if (result.indexOf(Constants.LOWER_NEGATIVE) > -1 || result.indexOf(Constants.UPPER_NEGATIVE) > -1
                || result.indexOf(Constants.CH_NEGATIVE) > -1) {
            returnResult = "阴性";
        } else {
            returnResult = result;
        }
        return returnResult;
    }
}
